<?php
    class Layout {

        private $ci;
        private $tpl_data;

        function __construct() {
            $this->ci = &get_instance();
            $this->ci->load->library('Tree', array('table' => 'tree'));
            //$this->ci->load->model('menu');
            $this->ci->load->library('Menu');
        }

        function load($view, $view_data = array()) {

            $config = $this->ci->system->getConfig();

            $theme = $config['THEME'].'/';
            $user = $this->ci->user->load();
			$config['table'] = 'tree'; // The name of the DB table to use.
			$config['pk'] = 'id'; // The name of the primary key column for the DB table
			$config['fk'] = 'parent_id'; // The name of the foreign key column for the DB table
			$config['text_column'] = 'title'; // The name of the column which data will be used as
			                                 // labels for the tree nodes.
			$config['toggler_open_class'] = 'toggler_open'; // The CSS class that will be assigned to
			                                                // parent nodes (nodes with children)
			$config['submenu_class'] = 'sub_menu'; // The CSS class that will be assigned to the UL
			                                       // sub-lists elements containing the child nodes.
			$config['menu_list_class'] = 'cat_menu'; // The CSS class that will be assigned to the
			                                         // whole category menu.            
            //$menu = $this->ci->menu->getTree();
            $this->ci->load->library('tree_menu');
            $this->ci->tree_menu->initialize($config);
            $this->ci->tree_menu->build();
            $menu = $this->ci->tree_menu->print_menu();

            //Assigning global template vars
            $this->tpl_data = array(
                'VIEW' => $theme.$view,
                'USER' => $user,
                'MAINMENU' => $menu,                
                'GUEST' => ($this->ci->system->getRole('rolename') == 'guest' ? TRUE : FALSE),
                'CLIENT' => ($this->ci->system->getRole('rolename') == 'client' ? TRUE : FALSE),
                'MANAGER' => ($this->ci->system->getRole('rolename') == 'manager' ? TRUE : FALSE),
                'ADMINISTRATOR' => ($this->ci->system->getRole('rolename') == 'administrator' ? TRUE : FALSE),
            );
                        

            $this->tpl_data = array_merge($this->tpl_data, $view_data);
            //$this->ci->load->vars($this->tpl_data);
            $output = $this->ci->load->view($theme.'maket', $this->tpl_data, TRUE);
            $this->ci->output->set_output($output);
        }
    }
?>